package com.solcoo.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.solcoo.dao.ZhangHuDao;
import com.solcoo.json.VIPJson;
import com.solcoo.service.DataService;
import com.solcoo.service.YongHuService;

@Controller
public class ZhangHuController extends AbstractController {
	
    @Resource
    private ZhangHuDao zhangHuDao;
    @Resource
    private YongHuService yongHuService;
    @Resource
    private DataService dataService;

    private final static String REPORT_PATH = "static/pic/report/";
    private static Logger logger = Logger.getLogger(YongHuController.class);

    /**
     * 取消关注
     */
    @RequestMapping(value="cacelRelation.do")
    public @ResponseBody JsonResult cacelRalation(Integer id,Integer merchantId) throws Exception {
        boolean operationSucc = yongHuService.cacelRelation(id, merchantId);
        if(operationSucc) {
            return this.getSuccessJsonResult("操作成功");
        } else {
            return this.getFailJsonResult("操作失败,不存在该关注，无法取消");
        }
    }

    /**
     * 个人关注商户
     */
    @RequestMapping(value="newMembership.do")
    public @ResponseBody JsonResult newMembership(Integer id,Integer merchantId) throws Exception {
        boolean operationSucc = false;
        operationSucc = yongHuService.newMembership(id, merchantId);
        if(operationSucc) {
            return this.getSuccessJsonResult("操作成功");
        } else {
            return this.getFailJsonResult("操作失败");
        }

    }

    @RequestMapping(value="queryVIP.jspDo")
    public ModelAndView getVIPList(Integer shanghuid, HttpServletRequest request) throws Exception {
    	String picPath = request.getSession().getServletContext().getRealPath(REPORT_PATH) + "/data/";
    	
    	dataService.createVIPCountByDay30(picPath, shanghuid);
    	
    	ModelAndView view = new ModelAndView();
    	
    	List<VIPJson> vipList = null;
    	
    	try {
    		vipList = zhangHuDao.getVIPListByShanghuid(shanghuid);
    	} catch (Exception e) {
        	logger.error(e.getMessage(), e);
    		e.printStackTrace();
    	}
    	
    	int memberCount =  yongHuService.getMemberCount(shanghuid);
    	
    	view.addObject("shanghuid", shanghuid);
    	view.addObject("vips", vipList);
		view.addObject("hytotal", vipList.size()); // 会员人数
		view.addObject("memberCount", memberCount); // 关注人数
		
    	view.setViewName("vipInfos");
    	
    	return view;
    }
    
}
